using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
public class Member_Shop_TryProductEdit : Member, IRequiresSessionState
{
	protected ScriptManager ScriptManager1;
	protected TextBox ProductNameBox;
	protected Label LbProError;
	protected DropDownList DropClass;
	protected DropDownList DropProvince;
	protected DataList DataList1;
	protected UpdatePanel UpdatePanel1;
	protected FileUpload FileUpload1;
	protected Label lbFileError;
	protected RadioButtonList RadioButtonList1;
	protected TextBox ProductNumBox;
	protected Label lbNumError;
	protected TextBox PriceBox;
	protected Label lbPriceError;
	protected TextBox DisCountBox;
	protected Label lbDisError;
	protected TextBox PostageBox;
	protected Label lbPostError;
	protected Label lbBeginError;
	protected Label lbEndError;
	protected TextBox FCKeditor1;
	protected Label lbErrorNum;
	protected Button ButEdit;
	protected int ProductID = 0;
	public string BeginDate = "";
	public string EndDate = "";
	public string StrImagePath = "";
	public string StrProvince = "";
	public string ShowPlace = "";
	protected DataTable dt = null;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.user.CheckUserLogin())
		{
			this.WriteErrMsg("", Language.GetMemberMsg("Common/NotLogin", new string[0]), this.InstallDir + "Passport/Login.aspx?ComeUrl=" + base.Server.UrlEncode(Fetch.CurrentUrl));
		}
		if (base.Request.QueryString["ProductID"] != null)
		{
			try
			{
				this.ProductID = int.Parse(base.Request.QueryString["ProductID"]);
			}
			catch (Exception ex)
			{
				ex.WriteErrorLog(new string[]
				{
					"正常操作"
				});
				this.ProductID = 0;
			}
		}
		this.dt = this.conn.GetDataTable("select * from HLShop_TryProduct where TryID=" + this.ProductID);
		if (!base.IsPostBack)
		{
			this.BindShopClass();
			this.BindProvince();
			this.ShowEditProductInfo();
			this.BindCity(this.DropProvince.SelectedValue);
		}
		this.FileUpload1.Attributes.Add("onchange", "previewImg(this.value);");
	}
	public void ShowEditProductInfo()
	{
		if (this.dt != null && this.dt.Rows.Count > 0)
		{
			this.ProductNameBox.Text = this.dt.Rows[0]["ProductName"].ToString();
			this.DropClass.SelectedValue = this.dt.Rows[0]["ClassID"].ToString();
			this.ShowPlace = this.dt.Rows[0]["ProductPlace"].ToString();
			if (this.ShowPlace.Split(new char[]
			{
				','
			})[0] != "全国")
			{
				this.DropProvince.SelectedValue = this.ShowPlace.Split(new char[]
				{
					','
				})[0];
			}
			else
			{
				this.DropProvince.SelectedIndex = 0;
			}
			this.StrImagePath = this.dt.Rows[0]["ProductImagePath"].ToString();
			this.RadioButtonList1.SelectedValue = this.dt.Rows[0]["ConsignmentType"].ToString();
			this.ProductNumBox.Text = this.dt.Rows[0]["ProductNum"].ToString();
			this.PriceBox.Text = this.dt.Rows[0]["marketPrice"].ToString();
			this.DisCountBox.Text = this.dt.Rows[0]["DiscountPrice"].ToString();
			this.PostageBox.Text = this.dt.Rows[0]["Postage"].ToString();
			this.BeginDate = this.dt.Rows[0]["BeginDate"].ToString();
			this.EndDate = this.dt.Rows[0]["Enddate"].ToString();
			this.FCKeditor1.Text = this.dt.Rows[0]["ProductIntroduction"].ToString();
		}
	}
	public void BindShopClass()
	{
		DataTable dataTable = this.conn.GetDataTable("select * from HLShop_Class where ParentID=0");
		foreach (DataRow dataRow in dataTable.Rows)
		{
			ListItem item = new ListItem(dataRow["ClassName"].ToString(), "0");
			this.DropClass.Items.Add(item);
			this.CategoryBind(int.Parse(dataRow["ClassID"].ToString()));
		}
		dataTable.Dispose();
	}
	public void CategoryBind(int ClassID)
	{
		DataTable dataTable = this.conn.GetDataTable("select * from HLShop_Class where ParentID=" + ClassID);
		if (dataTable != null && dataTable.Rows.Count > 0)
		{
			foreach (DataRow dataRow in dataTable.Rows)
			{
				ListItem item = new ListItem("-----" + dataRow["ClassName"].ToString(), dataRow["ClassID"].ToString());
				this.DropClass.Items.Add(item);
			}
		}
		dataTable.Dispose();
	}
	public void BindProvince()
	{
		DataTable dataTable = this.conn.GetDataTable("select Province from HLShop_CityArea group by Province");
		this.DropProvince.DataSource = dataTable;
		this.DropProvince.DataTextField = "Province";
		this.DropProvince.DataValueField = "Province";
		this.DropProvince.DataBind();
		this.DropProvince.SelectedIndex = 0;
		dataTable.Dispose();
	}
	public void BindCity(string Value)
	{
		DataTable dataTable = this.conn.GetDataTable("select City from HLShop_CityArea where Province='" + Value + "'");
		if (dataTable != null && dataTable.Rows.Count > 0)
		{
			this.StrProvince = Value;
			this.DataList1.DataSource = dataTable;
			this.DataList1.DataBind();
		}
		dataTable.Dispose();
	}
	public string IsSelectCity(string CityValue)
	{
		string result = "checked";
		string[] array = this.ShowPlace.Split(new char[]
		{
			','
		});
		if (array[0] != "全国")
		{
			for (int i = 1; i < array.Length; i++)
			{
				if (array[i] != CityValue)
				{
					result = "checked";
					break;
				}
			}
		}
		return result;
	}
	protected void DropProvince_SelectedIndexChanged(object sender, EventArgs e)
	{
		this.BindCity(this.DropProvince.SelectedValue);
	}
	public string GetImagePath()
	{
		string result;
		if (this.FileUpload1.HasFile)
		{
			string fileName = this.FileUpload1.PostedFile.FileName;
			string text = fileName.Substring(fileName.LastIndexOf("\\") + 1);
			string text2 = fileName.Substring(fileName.LastIndexOf(".") + 1);
			if (text2.ToUpper() == "JPG" || text2.ToUpper() == "GIF" || text2.ToUpper() == "PNG")
			{
				UpLoadFile upLoadFile = new UpLoadFile();
				if (this.FileUpload1.PostedFile.ContentLength > Convert.ToInt32(base.user.GetGroupPower("AllowUploadSize")) * 1024)
				{
					this.WriteErrMsg("", Language.Get("Shop/Product/UpLoadTypeLarget", base.user.GetGroupPower("AllowUploadSize")), "");
				}
				result = upLoadFile.UploadTryProduct(this.FileUpload1, this.config, "");
			}
			else
			{
				result = "格式错误";
			}
		}
		else
		{
			result = "";
		}
		return result;
	}
	protected void ButEdit_Click(object sender, EventArgs e)
	{
		int num = 0;
		string text = this.ProductNameBox.Text;
		if (text == "")
		{
			this.LbProError.Text = "产品名称不能为空！";
			num++;
		}
		else
		{
			if (text.Length > 100)
			{
				this.LbProError.Text = "产品名称不能超过100字！";
				num++;
			}
			else
			{
				this.LbProError.Text = "";
			}
		}
		int num2 = 0;
		try
		{
			num2 = int.Parse(this.ProductNumBox.Text);
		}
		catch (Exception ex)
		{
			ex.WriteErrorLog(new string[]
			{
				"正常操作"
			});
			num2 = 0;
		}
		if (num2 <= 0)
		{
			this.lbNumError.Text = "产品数量必须为大于0的整数";
			num++;
		}
		else
		{
			this.lbNumError.Text = "";
		}
		decimal d = 0m;
		try
		{
			d = decimal.Parse(this.PriceBox.Text);
		}
		catch (Exception ex)
		{
			ex.WriteErrorLog(new string[]
			{
				"正常操作"
			});
			d = 0m;
		}
		if (d <= 0m)
		{
			this.lbPriceError.Text = "市场价必须为大于0的数";
			num++;
		}
		else
		{
			this.lbPriceError.Text = "";
		}
		decimal d2 = 0m;
		try
		{
			d2 = decimal.Parse(this.DisCountBox.Text);
		}
		catch (Exception ex)
		{
			ex.WriteErrorLog(new string[]
			{
				"正常操作"
			});
			d2 = 0m;
		}
		if (d2 < 0m)
		{
			this.lbDisError.Text = "折扣价必须为大于等于0的数";
			num++;
		}
		else
		{
			this.lbDisError.Text = "";
		}
		decimal d3 = 0m;
		try
		{
			d3 = decimal.Parse(this.PostageBox.Text);
		}
		catch (Exception ex)
		{
			ex.WriteErrorLog(new string[]
			{
				"正常操作"
			});
			d3 = 0m;
		}
		if (d3 < 0m)
		{
			this.lbPostError.Text = "需付邮费必须大于等于0的数";
			num++;
		}
		else
		{
			this.lbPostError.Text = "";
		}
		DateTime t = DateTime.Now.AddDays(100.0);
		if (base.Request.Form["BeginDate"] != "" && base.Request.Form["BeginDate"] != null)
		{
			t = DateTime.Parse(base.Request.Form["BeginDate"]);
			this.lbBeginError.Text = "";
		}
		else
		{
			num++;
			this.lbBeginError.Text = "开始时间不能为空！";
		}
		DateTime dateTime = DateTime.Now;
		if (base.Request.Form["EndDateBox"] != "" && base.Request.Form["EndDateBox"] != null)
		{
			dateTime = DateTime.Parse(base.Request.Form["EndDateBox"]);
			this.lbEndError.Text = "";
		}
		else
		{
			num++;
			this.lbEndError.Text = "结束时间不能为空！";
		}
		if (dateTime < DateTime.Now)
		{
			this.lbEndError.Text = "结束时间必须大于等于现在的时间";
			num++;
		}
		else
		{
			if (t > dateTime)
			{
				if (this.lbBeginError.Text == "")
				{
					this.lbBeginError.Text = "开始时间不能大于结束时间";
					num++;
				}
			}
			else
			{
				this.lbBeginError.Text = "";
			}
		}
		string productImagePath = "";
		if (this.GetImagePath() == "格式错误")
		{
			this.lbFileError.Text = "图片格式错误！";
			num++;
		}
		else
		{
			if (this.GetImagePath() != "")
			{
				productImagePath = this.GetImagePath();
				this.lbFileError.Text = "";
			}
			else
			{
				productImagePath = this.dt.Rows[0]["ProductImagePath"].ToString();
				this.lbFileError.Text = "";
			}
		}
		if (num > 0)
		{
			this.lbErrorNum.Text = "共：" + num.ToString() + "处错误！";
		}
		else
		{
			ProductTry productTry = new ProductTry();
			if (base.Request.QueryString["ProductID"] != null)
			{
				try
				{
					productTry.TryID = int.Parse(base.Request.QueryString["ProductID"]);
				}
				catch (Exception ex)
				{
					ex.WriteErrorLog(new string[]
					{
						"正常操作"
					});
					productTry.TryID = 0;
				}
			}
			string productPlace;
			if (Fetch.GetOrPost("CheckCity") != null)
			{
				productPlace = this.DropProvince.SelectedValue + "," + Fetch.GetOrPost("CheckCity");
			}
			else
			{
				if (Fetch.GetOrPost("CheckProvince") != null)
				{
					productPlace = Fetch.GetOrPost("CheckProvince");
				}
				else
				{
					productPlace = "全国";
				}
			}
			productTry.ProductName = this.ProductNameBox.Text;
			productTry.ClassID = int.Parse(this.DropClass.SelectedValue);
			productTry.ProductPlace = productPlace;
			productTry.ProductImagePath = productImagePath;
			productTry.ConsignmentType = this.RadioButtonList1.SelectedValue;
			productTry.ProductNum = int.Parse(this.ProductNumBox.Text);
			productTry.marketPrice = decimal.Parse(this.PriceBox.Text);
			productTry.Postage = decimal.Parse(this.PostageBox.Text);
			productTry.DiscountPrice = decimal.Parse(this.DisCountBox.Text);
			productTry.BeginDate = Convert.ToDateTime(base.Request.Form["BeginDate"]);
			productTry.EndDate = Convert.ToDateTime(base.Request.Form["EndDateBox"]);
			productTry.ProductIntroduction = this.FCKeditor1.Text;
			int num3 = productTry.UpdateProductTry(productTry.TryID, productTry.ProductName, productTry.ClassID, productTry.ProductPlace, productTry.ProductImagePath, productTry.ConsignmentType, productTry.ProductNum, productTry.marketPrice, productTry.DiscountPrice, productTry.Postage, productTry.BeginDate, productTry.EndDate, productTry.ProductIntroduction);
			if (num3 == 1)
			{
				this.WriteSuccessMsg("", Language.Get("Shop/Product/OKEdit"), "Shop_TryManagement.aspx");
			}
			else
			{
				this.WriteErrMsg("", Language.Get("Common/Fail", "0"), "");
			}
		}
	}
}
